# -*- coding: utf-8 -*-
#############
#
#2015/6/17  judy
#yz.sale.order表的partner_id字段与res.partner表的sale_order_ids字段是many2one关系
#
#fighting
#############

from openerp.osv import fields,osv

class res_partner(osv.osv):
    _inherit = 'res.partner'
   
    def _sale_order_count(self,cr,uid,ids,field_name,arg,context=None):
        res = dict(map(lambda x: (x,0), ids))
        #当前用户可能没有访问销售订单的权限
        try:
            for partner in self.browse(cr,uid,ids,context):
                res[partner.id] = len(partner.sale_order_ids)#求当前用户的销售订单行数
        except:
            pass
        return res  #{(key,value),(ids,length)}
    
    _columns = {
                'sale_order_ids':fields.one2many('yz.sale.order','partner_id',u'销售订单'),
                'sale_order_count':fields.function(_sale_order_count, string='# of Sales Order',type='integer'),
                }